//################################
// 版权所有：鸽子游戏俱乐部
// 系统名称：鸽子游戏俱乐部-移动版
// 文件名称：emailService.js
// 创建时间：2016年07月03日
// 创建人员：Panda
// 功能说明：email service
//################################

'use strict';

import Config from '../common/config';
import CommonFunc from '../common/commonFunc';

import { request } from '../utils/requestUtil';

import UIConstants from '../common/uiconstants';

function getEmailList(emailType, index, count) {
    return new Promise((resolve, reject) => {
        var result = {
            status: true,
            emailList: [{"isread":false,"respondedTo":2,"subject":"测试","body":"内容测试","from":"LEMO","sendTo":"ABC","postedDate":"201612081454"},{"isread":true,"respondedTo":1,"subject":"测试1","body":"内容测试1","from":"JACK","sendTo":"ABC","postedDate":"201612071454"}],
        };

        resolve(result);
        /*
        var requestUrl = CommonFunc.getUrlConfig('inboxMailListUrl','email');

        switch (emailType) {
            case (UIConstants.email.inbox):
                requestUrl = CommonFunc.getUrlConfig('inboxMailListUrl','email');
                break;
            case (UIConstants.email.outbox):
                requestUrl = CommonFunc.getUrlConfig('outboxMailListUrl','email');
                break;
            case (UIConstants.email.draftbox):
                requestUrl = CommonFunc.getUrlConfig('draftboxMailListUrl','email');
                break;
            default:
                break;
        }

        let params = {};
        params.timeout = 10000;
        params.method = 'get';
        params.url = requestUrl.replace("{START}", index).replace("{COUNT}", count);
    
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                    emailList: responseData.data,
                };

                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };

                reject(result);
            }

        })
        .catch((error) => {
            reject(error);
        });*/
    });
}

function getEmailDetail(id) {
    return new Promise((resolve, reject) => {
        var requestUrl = CommonFunc.getUrlConfig('mailInfoUrl','email').replace("{MAILID}", id);

        let params = {};
        params.timeout = 10000;
        params.method = 'get';
        params.url = requestUrl;
    
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                    email: responseData.data,
                };

                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };

                reject(result);
            }

        })
        .catch((error) => {
            reject(error);
        });
    });
}

function getEmailCounts(){
    return new Promise((resolve, reject) => {
       var requestUrl = CommonFunc.getUrlConfig('getUnReadCountUrl','email');

        let params = {};
        params.timeout = 10000;
        params.method = 'get';
        params.url = requestUrl;
    
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                    data: responseData.data,
                };
                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };
                reject(result);
            }
        })
        .catch((error) => {
            reject(error);
        });
    });
}

function getEmailAttachmentDetail(attachment) {
    return new Promise((resolve, reject) => {
        let attachmentUrl = encodeURIComponent(decodeURIComponent(attachment.url));
        // let encodedFileName = encodeURIComponent(attachment.fileName);
        // attachmentUrl = encodeURIComponent(attachmentUrl + '&fileName=' + encodedFileName);

        let params = {};
        params.timeout = 10000;
        params.method = 'get';
        params.url = CommonFunc.getUrlConfig('getSubAttachments', 'email').replace('{SOURCEURL}', attachmentUrl).replace('{MODULEID}', attachment.moduleId);

        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                    data: responseData.data,
                };
                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };
                reject(result);
            }
        })
        .catch((error) => {
            reject(error);
        });
    });
}

function deleteMail(mailId){
    return new Promise((resolve, reject) => {
       var requestUrl = CommonFunc.getUrlConfig('deleteMailUrl','email');

        let params = {};
        params.timeout = 10000;
        params.method = 'post';
        params.postType='string';
        params.params=mailId;
        params.url = requestUrl;
    
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                    data: responseData.data,
                };
                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };
                reject(result);
            }
        })
        .catch((error) => {
            reject(error);
        });
    });
}

function deleteMailList(mailIdList){
    return new Promise((resolve, reject) => {
       var requestUrl = CommonFunc.getUrlConfig('delelteAllUrl','email');

        let params = {};
        params.timeout = 10000;
        params.method = 'post';
        params.postType='json';
        params.params=mailIdList;
        params.url = requestUrl;
    
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                };
                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };
                reject(result);
            }
        })
        .catch((error) => {
            reject(error);
        });
    });
}

function getEmailFrequentContacts() {
    return new Promise((resolve, reject) => {
        let params = {};
        params.timeout = 10000;
        params.method = 'get';
        params.url = CommonFunc.getUrlConfig('mailFreqContactListUrl', 'email');

        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                    data: responseData.data,
                };
                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };
                reject(result);
            }
        })
        .catch((error) => {
            reject(error);
        });
    });
}

//实时搜索
function searchEmailByKey(emailType,searchNO,key) {
    return new Promise((resolve, reject) => {
       var requestUrl = CommonFunc.getUrlConfig('contactByKeyWordUrl',emailType).replace('{NO}',searchNO).replace("{KEY}", encodeURIComponent(encodeURIComponent(key)));
        let params = {};
        params.timeout = 10000;
        params.method = 'get';
        params.url = requestUrl;
    
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                    data: responseData.data,
                };
                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };
                reject(result);
            }
        })
        .catch((error) => {
            reject(error);
        });
    });
}
// 发送
function sendMail(mailData,flag) {
    return new Promise((resolve, reject) => {
        var requestUrl = CommonFunc.getUrlConfig('sendMailWithAttachmentUrl','email').replace('{FLAG}', flag);
 
        let params = {};
         params.timeout = 10000;
        params.method = 'post';
        params.postType = 'json';
        params.url = requestUrl;
        params.params = mailData;
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                };

                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                    code: responseData.code ? responseData.code : '',
                    data: (responseData.code && responseData.code === '10100-002') ? responseData.data : null,
                };
                reject(result);
            }

        })
        .catch((error) => {
            reject(error);
        });
    });
}

//setResponseTo
function setResponseTo(id,flag) {
    return new Promise((resolve, reject) => {
        var requestUrl = CommonFunc.getUrlConfig('setRespondedTo','email').replace('{MAILID}', id);
 
        let params = {};
         params.timeout = 10000;
        params.method = 'post';
        params.postType = 'string';
        params.url = requestUrl;
        params.params = flag;
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                };

                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };
                reject(result);
            }

        })
        .catch((error) => {
            reject(error);
        });
    });
}
//保存草稿
function saveDraft(mailData,deleteId) {
    return new Promise((resolve, reject) => {
        var requestUrl = CommonFunc.getUrlConfig('saveDraftUrl','email').replace('{DELETEID}', deleteId);
 
        let params = {};
        params.timeout = 10000;
        params.method = 'post';
        params.postType = 'json';
        params.url = requestUrl;
        params.params = mailData;
        request(params)
        .then((responseData) => {
            if (responseData.status) {
                var result = {
                    status: responseData.status,
                };
                resolve(result);
            }
            else {
                var result = {
                    status: responseData.status,
                    message: responseData.message,
                };
                reject(result);
            }
        })
        .catch((error) => {
            reject(error);
        });
    });
}
module.exports = {
    getEmailList,
    getEmailDetail,
    getEmailCounts,
    getEmailAttachmentDetail,
    deleteMail,
    deleteMailList,
    getEmailFrequentContacts,
    searchEmailByKey,
    sendMail,
    saveDraft,
    setResponseTo,
};